package com.cc.bsp.persistence.hibernate.expand.namedql.content;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import com.cc.bsp.persistence.exception.NamedQLException;
import com.cc.bsp.persistence.exception.PersistenceException;
import com.cc.bsp.persistence.hibernate.expand.namedql.builder.DynamicHibernateStatementBuilder;

public class NamedQLContent implements BeanFactoryAware{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(NamedQLContent.class );
	
	private static Map<String, String> namedHQLQueries = new HashMap<String, String>();

	private static Map<String, String> namedSQLQueries = new HashMap<String, String>();
	
	private static BeanFactory beanFactory = null;

	private DynamicHibernateStatementBuilder hibernateStatementBuilder = null;
	
	private static NamedQLContent instance = null;
	
	private NamedQLContent(){
	
	}
	
	public static NamedQLContent getInstance(){
		if(instance ==null){
			instance = (NamedQLContent) beanFactory.getBean("namedQLContent");
			instance.initialize();
		}
		return instance;
	}

	private DynamicHibernateStatementBuilder getHibernateStatementBuilder() {
		return hibernateStatementBuilder;
	}

	public void setHibernateStatementBuilder(
			DynamicHibernateStatementBuilder hibernateStatementBuilder) {
		this.hibernateStatementBuilder = hibernateStatementBuilder;
	}
	
	
	public void reload(){
		try {
			getHibernateStatementBuilder().init();
			namedHQLQueries.clear();
			namedSQLQueries.clear();
			Map<String, String> tempHQL = getHibernateStatementBuilder().getNamedHQLQueries();
			Map<String, String> tempSQL = getHibernateStatementBuilder().getNamedSQLQueries();
			namedHQLQueries.putAll(tempHQL);
			namedSQLQueries.putAll(tempSQL);
		} catch (IOException e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage());
		} catch (NamedQLException e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage());
		}
	}
	
	public void initialize() {
		Map<String, String> tempHQL = getHibernateStatementBuilder().getNamedHQLQueries();
		Map<String, String> tempSQL = getHibernateStatementBuilder().getNamedSQLQueries();
		namedHQLQueries.putAll(tempHQL);
		namedSQLQueries.putAll(tempSQL);
	}

	public void addHQL(String sqlName, String sqlstr)
			throws PersistenceException {

		if (namedHQLQueries.containsKey(sqlName)) {
			throw new PersistenceException("sqlName is exits");
		}

		namedHQLQueries.put(sqlName, sqlstr);
	}

	public void addSQL(String sqlName, String sqlstr)
			throws PersistenceException {
		if (namedSQLQueries.containsKey(sqlName)) {
			throw new PersistenceException("sqlName is exits");
		}
		namedSQLQueries.put(sqlName, sqlstr);
	}
	
	public String getHQL(String sqlName){
		return namedHQLQueries.get(sqlName);
	}
	
	public String getSQL(String sqlName){
		return namedSQLQueries.get(sqlName);
	}

	@SuppressWarnings("static-access")
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

}
